var  loginData = [
        {name:"123",password:"123456"},
        {name:'悟空',password:'1314'}
    ];



//登录检测
(function(){
 var btn_box=null,//按钮框
     dl_btn=null,//登录按钮
     zc_btn=null,//注册按钮
     new_id=null,//注册框
     password=null,//登录框
     login_input=null,
     name=null,
     passtext=null,
     hint=null;
    
     var initData = function(){
        btn_box=document.getElementsByClassName('btn-box')[0],//按钮框
        dl_btn=btn_box.getElementsByClassName('dl-btn')[0],//登录按钮
        zc_btn=null,//注册按钮
        new_id=document.getElementsByClassName('new-id')[0],//注册框
        password=document.getElementsByClassName('password')[0],//登录框
        login_input=document.getElementsByClassName('login-input')[0],
        name=document.getElementsByClassName('name')[0],
        passtext=document.getElementsByClassName('pass')[0],
        hint=document.getElementsByClassName('hint')[0];
};

  var new_login=function(){
      new_id.addEventListener('click',function(){
      
        login_input.innerHTML=`
        <p class="hint">❗用户名已注册</p>
        <div class="input-box name-box zc"> 
        <label class="name-label"></label>
        <input type="text" class="name" placeholder="会员名/邮箱/手机号">
    </div>
    <div class="input-box pass-box zc"> 
        <label class="pass-label"></label>
        <input type="password" name="password" class="pass" placeholder="设置密码">
    </div>
    <div class="input-box btn-box "> 
        
        <input type="button" value="注册" class="zc-btn">
    </div>
       
        `;

        initData();
        zc_btn = btn_box.getElementsByClassName('zc-btn')[0];
        newLogIn();
 });
};

var password_login=function(){
    password.addEventListener('click',function(){
        zc_btn = null;
        login_input.innerHTML=`
        <p class="hint">❗登录名或密码不正确</p>
        <div class="input-box name-box dl"> 
        <label class="name-label" for="name"></label>
        <input type="text" class="name" id="name"placeholder="会员名/邮箱/手机号">
    </div>
    <div class="input-box pass-box dl"> 
        <label class="pass-label" for="pass"></label>
        <input type="password" name="password" class="pass" id="pass"placeholder="请输入登录密码">
    </div>
    <div class="input-box btn-box " > 
        
        <input type="button" value="登录" class="dl-btn">
    </div>
        `;
        initData();
        logIn();//登录
    })
    
};

var logIn = function(){
   dl_btn.addEventListener('click',function(){
       for(var i = 0; i < loginData.length; i ++){
            if(name.value === loginData[i].name && passtext.value === loginData[i].password){
               dl_btn.value="登陆成功";
                hint.style.display = "none";
                 return;
            }else if(i === loginData.length-1 && name.value !== loginData[i].name ||  passtext.value !== loginData[i].password){
              hint.style.display="block";
              dl_btn.value="登录";
            }  
       };
   });

};

var newLogIn=function(){

    if(zc_btn){
        zc_btn.addEventListener('click',function(){
            for(var i = 0; i < loginData.length; i ++){
                if(name.value === loginData[i].name){
                   
                    hint.style.display="block";
                    zc_btn = "注册";
                    return ;
                }else if(i === loginData.length-1 && name.value !==loginData[i].name){
                   
                    zc_btn.value = "注册成功";
                    hint.style.display="none";
            };
        
            };
            loginData.push({name:name.value,password:passtext.value});
        });
    };
   
   
};

var init=function(){
   initData();
    password_login();//登录页面
    new_login();//注册页面
    logIn();//登录



};

return init();
}());